<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script type="text/javascript">

    function send(msg) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(msg)
        }
    }

    var socket;
    //当前页面一打开就会执行的
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:6666/some");
        socket.onopen = function (ev) {
            //在右侧文本域中显示连接建立提示
            var ta = document.getElementById("responseText");
            ta.value = "连接已建立";
            //当接收到服务端发送的消息时会触发onmessage的执行
            socket.onmessage = function (ev) {
                //将服务端发送来的消息在右侧文本域中显示，在原有内容基础上进行拼接
                var ta = document.getElementById("responseText");
                ta.value = ta.value + "\n" + ev.data;

                //将连接关闭消息在右侧文本域中显示，在原有内容基础上进行拼接
                socket.onclose = function (ev) {
                    var ta = document.getElementById("responseText");
                    ta.value = ta.value + "\n连接已关闭";
                }
            }
        }
    } else {
        alert("不支持websocket")
    }

</script>
<body>
<form>
    <textarea id="message" style="width: 150px; height: 150px"></textarea>
    <input type="button" value="发送" onclick="send(this.form.message.value)">
    <textarea id="responseText" style="width: 150px; height: 150px"></textarea>
</form>

</body>
</html>